<?php
namespace app\appoint\controller;
use app\ApiRest;
use app\appoint\model\Question;
use app\appoint\model\QuestionList;
use app\appoint\model\QuestionPv;
use app\appoint\model\QuestionText;
use app\appoint\model\QuestionUserText;
use think\App;
use think\facade\Db;


class IndexQuestion extends ApiRest
{

    protected $model;
    //预约服务模型
    protected $list_model;

    protected $text_model;

    protected $user_text_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model      = new Question();

        $this->list_model = new QuestionList();

        $this->text_model = new QuestionText();

        $this->user_text_model = new QuestionUserText();

    }

    /**
     * @author chenniang
     * @DataTime: 2021-01-25 16:02
     * @功能说明:问卷列表
     */
    public function questionList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];
        }

        $data = $this->model->dataList($dis,10);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-27 11:01
     * @功能说明:问卷详情
     */

    public function questionInfo(){

        $input = $this->_param;

        $data  = $this->model->dataInfo(['id'=>$input['id']]);
        //查询有没有题目
        $dis = [

            'q_id'    => $data['id'],

            'text_id' => 0
        ];

        $list = $this->list_model->where($dis)->find();

        $data['have_text'] = !empty($list)?1:0;
        //增加浏览量
        $pv_model = new QuestionPv();

        $pv_model->dataAdd($this->_uniacid,$data['id']);
        //用户有没有答过该题
        $have_question = $this->user_text_model->dataInfo(['user_id'=>$this->getUserId(),'status'=>1,'que_id'=>$data['id']]);

        $data['have_question'] = !empty($have_question)?1:0;

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-26 14:55
     * @功能说明:查询题目 开始答题的时候返回的 默认查询第一个
     */
    public function selectQueText(){

        $input = $this->_input;
        //查询第一个
        $dis = [

            'q_id'    => $input['que_id'],

            'text_id' => 0
        ];

        $data = $this->list_model->where($dis)->order('top desc,id desc')->find();

        if(empty($data)){

            return $this->success($data);

        }

        $data = $data->toArray();

        $data['icon']      = 1;
        //查询每道题的内容
        $data['data_text'] = $this->text_model->textInfo($data);
        //是否是最后一道题
        $data['is_end']    = empty($data['next_id'])?1:0;

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-26 13:59
     * @功能说明:开始答题
     */
    public function startText(){

        $input = $this->_input;
        //问卷详情
        $que = $this->model->dataInfo(['id'=>$input['id']]);

        $insert = [

            'uniacid' => $this->_uniacid,

            'user_id' => $this->getUserId(),

            'que_id'  => $que['id'],

            'title'   => $que['title'],

            'staff_id' => $input['staff_id'],
        ];

        $res = $this->user_text_model->dataAdd($insert);

        $id  = $this->user_text_model->getLastInsID();

        return $this->success($id);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-27 13:35
     * @功能说明:没答一到题就加入数据库
     */
    public function textAdd(){

        $input = $this->_input;
        //查询开始答题时候提交的问题
        $user_text = $this->user_text_model->dataInfo(['id'=>$input['user_text_id']]);

        $text = $this->list_model->dataInfo(['id'=>$input['text_id']]);

        if(empty($text)){

            $this->errorMsg('问题未找到');
        }

        $insert = [

            'uniacid' => $text['uniacid'],

            'q_id'    => $text['q_id'],

            'must'    => $text['must'],

            'title'   => $text['title'],

            'text'    => $text['text'],

            'type'    => $text['type'],

            'text_id' => $user_text['id'],

            'text_p_id' => $text['id'],

            'top'       => $input['icon'],

            'ans_text'  => !empty($input['ans_text'])?$input['ans_text']:'',
        ];

        Db::startTrans();

        $res = $this->list_model->insert($insert);

        if($res!=1){

            $this->errorMsg('回答失败');

        }

        $list_id = $this->list_model->getLastInsID();
        //问题详情
        $data_text = $this->text_model->textInfo($text);

        if(empty($data_text)&&$text['type']!=3){

            $this->errorMsg('问题未找到');

        }

        if($text['type']!=3){
            //添加题目的内容
            foreach ($data_text as $value){

                $insert = [

                    'uniacid' => $this->_uniacid,

                    'q_id'    => $list_id,

                    'text'    => $value['text'],

                    'type'    => $value['type'],

                    'q_type'  => $value['q_type'],

                    'is_jump' => $value['is_jump'],

                    'jump_id' => $value['jump_id'],

                    'text_id' => $user_text['id'],

                    'text_p_id' => $value['id'],
                    //用户提交的答案
                    'is_select' => in_array($value['id'],$input['ans_id'])?1:0

                ];

                $this->text_model->insert($insert);

                if($text['type']==1){

                    $id = $this->text_model->getLastInsID();

                    foreach ($value['ans'] as $v){

                        $insert = [

                            'uniacid'   => $this->_uniacid,

                            'q_id'      => $list_id,

                            'text'      => $v['text'],

                            'type'      => $v['type'],

                            'q_type'    => $v['q_type'],

                            'is_jump'   => $v['is_jump'],

                            'jump_id'   => $v['jump_id'],

                            'pid'       => $id,

                            'text_id'   => $user_text['id'],

                            'text_p_id' => $value['id'],
                            //用户提交的答案
                            'is_select' => in_array($v['id'],$input['ans_id'])?1:0

                        ];

                        $this->text_model->insert($insert);

                    }
                }

            }
        }
        //如果复合型的可能会根据答案来获取下一道题
        if($text['type']==2&&!empty($input['ans_id'])){

            $next_text_id = $this->text_model->dataInfo(['is_jump'=>1,'id'=>$input['ans_id'][0]]);

            $next_text_id = !empty($next_text_id)?$next_text_id['jump_id']:0;

        }
        //下一道题
        $next_id = !empty($next_text_id)?$next_text_id:$text['next_id'];
        //说明最后一题了
        if(empty($text['next_id'])||$next_id==-1){

            $this->user_text_model->dataUpdate(['id'=>$user_text['id']],['create_time'=>time(),'status'=>1]);
        }
        //查询下一道题
        if(!empty($next_id)&&$next_id>0){
            //查询指定的
            $dis = [

                'id' => $next_id
            ];

            $data = $this->list_model->where($dis)->find();

            if(empty($data)){

                return $this->success($data);

            }

            $data = $data->toArray();

            $data['icon']      = $input['icon']+1;
            //查询每道题的内容
            $data['data_text'] = $this->text_model->textInfo($data);
            //是否是最后一道题
            $data['is_end']    = empty($data['next_id'])?1:0;

        }

        Db::commit();

        return $this->success(!empty($data)?$data:[]);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-01-28 09:34
     * @功能说明:用户报名情况
     */
    public function userTextList(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];
        //问卷详情
        $data['info'] = $this->model->dataInfo($dis);

        $dis = [

            'a.que_id'   => $input['id'],

            'a.status'   => 1,

            'a.staff_id' => $this->getUserId()
        ];

        $data['list'] = $this->user_text_model->userTextList($dis);

        return $this->success($data);

    }





}
